package com.big.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.big.app.dao.SmsLogDao;
import com.big.app.entity.SmsLog;
import com.big.app.service.SmsLogService;
import com.big.app.util.JedisUtils;
import com.big.app.util.PassUtils;
import com.big.app.util.SmSCode;
import com.big.app.util.StringUtils;
import com.big.app.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SmsLogServiceImpl  implements SmsLogService {
    @Autowired
    private SmsLogDao dao;
    @Override
    public R sendLoginCode(String phone) {
        if (StringUtils.isNoyEmpty(phone)) {
            if (phone.matches("^1[3-9]{1}[0-9]{9}$")) {
                /*随机生成验证码*/
                int num = SmSCode.createNum(6);
                if(SmSCode.sendFCode(phone,num)){
                    //验证码发送成功,存入redis中，设置失效时间十分钟
                    JedisUtils.setStr("user_code_login_"+phone,num+"",600);
                    return new R(200,"ok","验证码发送成功");
                }
            }
        }
        return new R(400,"false","手机号码有误");
    }

    @Override
    public R queryAll(String phone) {
        QueryWrapper<SmsLog> query = new QueryWrapper<SmsLog>();
        if(StringUtils.isNoyEmpty(phone)) {
            query.eq("phone", phone);
        }
        List<SmsLog> smsLogs = dao.selectList(query);
        return new R(200,"ok",smsLogs);
    }
}
